#!/usr/bin/env bash
# @author: l3nn4rt

echo " $@ " | grep -qE " (-h|--help) " && {
    cat <<EOF
Usage: $0 [FILE] ...

Read one-line sequences from FILE(s) and search these in the blaster database.
Write on stdout in the format:
    FILE    LINE    SEQUENCE    RID

where:
    FILE, LINE and SEQUENCE refer to the input file
    RID is the request ID matching SEQUENCE

NOTE: this search is very basic: a one-line search sequence will match against
a database sequence if the former appears in the latter as a single line only!
EOF
    exit
}

values=$(mktemp)
# output format is: "file row sequence"
grep -Hrn '.*' $@ | grep -v :$ | tr ':' '\t' >$values

keys=$(mktemp)
# output format is: "request sequence"
grep -r '.*' blaster_requests/*/*.fasta | tr ':/' '\t' | cut -f 2,4 >$keys

# also print input one-line sequences without database matches
join -a 1 -1 3 -2 2 <(sort -k 3 $values) <(sort -k 2 $keys) \
    | awk '{print $2 "\t" $3 "\t" $1 "\t" $4}' | sort -V

rm $keys $values
